package com.wjk.easy.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.clj.fastble.data.BleDevice;
import com.wjk.easy.R;
import com.wjk.easy.db.ExamData;
import com.wjk.easy.utils.CommonUtil;
import com.wjk.easy.view.BlueScanListDialog;

import org.litepal.LitePal;
import org.litepal.crud.async.UpdateOrDeleteExecutor;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.util.List;

public class BlueDeviceScanAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<BleDevice> list;

    public BlueDeviceScanAdapter(Context context, List<BleDevice> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_blue_scan_data, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        // TextView tv_card_id, tv_weight, tv_create_time, tv_size;
        final BleDevice exam = list.get(position);
        MyViewHolder h = (MyViewHolder) holder;
        h.tv_device_name.setText(exam.getName()==null?exam.getMac():exam.getName());
        h.ll_data_container.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(itemClick!=null){
                    itemClick.onClick(exam);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    //  添加数据
    public void addData(int position, BleDevice data) {
        list.add(position, data);
        //添加动画
        notifyItemInserted(position);
    }

    public void setDataList(List<BleDevice> examList) {
        list.clear();
        list.addAll(examList);
        notifyDataSetChanged();
    }
    BlueScanListDialog.OnDeviceClickListener itemClick;
    public void setOnItemClickListener(BlueScanListDialog.OnDeviceClickListener itemClick) {
        this.itemClick = itemClick;
    }

    public void clear() {
        list.clear();
    }

    public void addUhfDevice(BluetoothDevice device) {
        BleDevice bleDevice = new BleDevice(device);
        if(!list.contains(bleDevice)) {
            list.add(bleDevice);
            notifyDataSetChanged();
        }
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_device_name;
        LinearLayout ll_data_container;

        MyViewHolder(View view) {
            super(view);
            tv_device_name = view.findViewById(R.id.tv_device_name);
            ll_data_container = view.findViewById(R.id.ll_data_container);
        }
    }
    EmptyListener emptyListener;
    public interface EmptyListener{
        void onEmpty();
    }
}
